package models

// 定义数据库连接名称常量
const dbDaxinxinbase = "daxinxinbase" // 假设使用 db_config1 作为固定的数据库连接

// DaijunUser 表示 daijun_user 表
type User struct {
	ID                 uint64 `gorm:"primary_key"`
	UserType           uint8  `gorm:"column:user_type"`
	Sex                int8   `gorm:"column:sex"`
	Birthday           int    `gorm:"column:birthday"`
	ContractTime       int    `gorm:"column:contract_time"`
	IDCard             string `gorm:"column:idcard"`
	Remark             string `gorm:"column:remark"`
	LastLoginTime      int    `gorm:"column:last_login_time"`
	Score              int    `gorm:"column:score"`
	Coin               uint   `gorm:"column:coin"`
	Balance            string `gorm:"column:balance"`
	CreateTime         int    `gorm:"column:create_time"`
	UserStatus         uint8  `gorm:"column:user_status"`
	UserLogin          string `gorm:"column:user_login"`
	UserPass           string `gorm:"column:user_pass"`
	UserNickname       string `gorm:"column:user_nickname"`
	UserEmail          string `gorm:"column:user_email"`
	UserURL            string `gorm:"column:user_url"`
	Avatar             string `gorm:"column:avatar"`
	Signature          string `gorm:"column:signature"`
	LastLoginIP        string `gorm:"column:last_login_ip"`
	UserActivationKey  string `gorm:"column:user_activation_key"`
	Mobile             string `gorm:"column:mobile"`
	More               string `gorm:"column:more"`
	Ssbm               int    `gorm:"column:ssbm"`
	Sszw               int    `gorm:"column:sszw"`
	Gangwei            string `gorm:"column:gangwei"`
	Shouji             string `gorm:"column:shouji"`
	Jjlxdh             string `gorm:"column:jjlxdh"`
	Bangongdianhua     string `gorm:"column:bangongdianhua"`
	Duanhao            string `gorm:"column:duanhao"`
	UserSSFGS          string `gorm:"column:user_ssfgs"`
	Beiwanglu          string `gorm:"column:beiwanglu"`
	Ssgs               uint   `gorm:"column:ssgs"`
	JituanID           int    `gorm:"column:jituanid"`
	Ishtgly            uint8  `gorm:"column:ishtgly"`
	Bieming            string `gorm:"column:bieming"`
	Iscsgly            uint8  `gorm:"column:iscsgly"`
	Islingdao          uint8  `gorm:"column:islingdao"`
	LingdaoGLBMID      string `gorm:"column:lingdao_glbmid"`
	BossOpenID         string `gorm:"column:boss_open_id"`
	BossToken          string `gorm:"column:boss_token"`
	BossPhone          string `gorm:"column:boss_phone"`
	UserWXOpenID       string `gorm:"column:user_wx_openid"`
	UserWXBindTime     int    `gorm:"column:user_wx_bindtime"`
	WebToken           string `gorm:"column:web_token"`
	IsJxtcry           uint8  `gorm:"column:is_jxtcry"`
	SuperID            uint64 `gorm:"column:super_id"`
	DepartmentLeaderID int    `gorm:"column:department_leader_id"`
	DepartmentID       uint16 `gorm:"column:department_id"`
	ConfirmStatus      uint8  `gorm:"column:confirm_status"`
	TeamID             uint64 `gorm:"column:team_id"`
	YejiArea           string `gorm:"column:yeji_area"`
	WXUnionID          string `gorm:"column:wx_unionid"`
	BossWXOpenID       string `gorm:"column:boss_wx_openid"`
	Zhiwei             string `gorm:"column:zhiwei"`
	Database           string `gorm:"-"`
}

// TableName 定义表名前缀
func (User) TableName() string {
	return "daijun_user"
}

// SetDB 设置数据库连接名称
func (u *User) SetDB(dbDaxinxinbase string) {
	u.Database = dbDaxinxinbase
}

// GetUserInfo 通过ID获取商业险信息
func GetUserInfo(id int) (user User, err error) {
	err = DBs[dbDaxinxinbase].First(&user, id).Error
	return user, err
}

// GetUsers 获取商业险列表
func GetUsers(pageNum int, pageSize int, maps interface{}) (users []User) {
	DBs[dbDaxinxinbase].Where(maps).Offset(pageNum).Limit(pageSize).Find(&users)
	return users
}

// UserList 获取商业险列表
func UserList(pageNum int, pageSize int, maps interface{}) (users []User) {
	err := DBs[dbDaxinxinbase].Where(maps).Offset(pageNum).Limit(pageSize).Find(&users).Error
	if err != nil {
		return nil
	}
	return users
}

// GetAllUser 获取当前表的所有数据
func GetAllUser() (users []User, err error) {
	err = DBs[dbDaxinxinbase].Find(&users).Error
	return users, err
}

// GetUser 通过ID获取商业险信息
func GetUser(id int) (user User, err error) {
	err = DBs[dbDaxinxinbase].First(&user, id).Error
	return user, err
}

// GetUserTotal 获取商业险总数
func GetUserTotal(maps interface{}) (count int, err error) {
	var total int64
	result := DBs[dbDaxinxinbase].Model(&User{}).Where(maps).Count(&total)
	if result.Error != nil {
		return 0, result.Error
	}
	return int(total), nil
}

// ExistUserByMaps 判断商业险是否存在
func ExistUserByMaps(maps interface{}) bool {
	var user User
	DBs[dbDaxinxinbase].Select("id").Where(maps).First(&user)
	return user.ID > 0
}

// UserExistTagByID 通过ID判断商业险是否存在
func UserExistTagByID(id int) bool {
	var user User
	DBs[dbDaxinxinbase].Select("id").Where("id = ?", id).First(&user)
	return user.ID > 0
}

// DeleteUser 删除商业险
func DeleteUser(maps interface{}) (bool, error) {
	if err := DBs[dbDaxinxinbase].Where(maps).Delete(&User{}).Error; err != nil {
		return false, err
	}
	return true, nil
}

// EditUser 编辑商业险
func EditUser(id int, data interface{}) (bool, error) {
	if err := DBs[dbDaxinxinbase].Model(&User{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return false, err
	}
	return true, nil
}
